#!/bin/bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions
cluster="some-name"

if version_gt "1.19"; then
	echo "Kubernetes version is 1.19+ so skipping this test because of pumba support."
	exit 0
fi

pumba_pod() {
    local action=$1
    local pod=$2

    # run chaos for Pod
    run_pumba $action "re2:$pod"
    sleep 2

    # check if all 3 Pods started
    wait_for_running $cluster-pxc 3
    sleep 20
    desc 'check data consistency for chaosed Pod'
    compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $pod.$cluster-pxc -uroot -proot_password"
}

pumba_all_pods() {
    local action=$1

    # run chaos for Pod
    run_pumba $action "re2:$cluster-*"
    sleep 2

    # check if all 3 Pods started
    wait_for_running $cluster-pxc 3
    sleep 30

    desc 'check data consistency for chaosed Pod'
    compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster-pxc-0.$cluster-pxc -uroot -proot_password"
    compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster-pxc-1.$cluster-pxc -uroot -proot_password"
    compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster-pxc-2.$cluster-pxc -uroot -proot_password"
}


pause_pod() {
    local pod=$1

    # run chaos for Pod
    kubectl_bin exec -it "$(get_pumba)" -- /pumba -l info pause --duration 1m "re2:$pod" &
    pid=$!
    sleep 20

    # write data
    run_mysql \
        'INSERT myApp.myApp (id) VALUES (100501)' \
        "-h $cluster-proxysql -uroot -proot_password"

    # wait for unpause
    wait $pid

    # check if all 3 Pods started
    wait_for_running $cluster-pxc 3
    sleep 20

    desc 'check data consistency for chaosed Pod'
    compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $pod.$cluster-pxc -uroot -proot_password" "-2nd"
}

netem_pod() {
    local pod=$1

    # run chaos for Pod
    kubectl_bin exec -it "$(get_pumba)" -- /pumba -l info netem --tc-image 'gaiadocker/iproute2' --duration 1m loss --percent 100 --correlation 100 "re2:$pod" &
    pid=$!
    sleep 20

    # write data
    run_mysql \
        'INSERT myApp.myApp (id) VALUES (100502)' \
        "-h $cluster-pxc -uroot -proot_password"

    # wait for unpause
    wait $pid || :

    # check if all 3 Pods started
    wait_for_running $cluster-pxc 3
    sleep 20

    desc 'check data consistency for chaosed Pod'
    compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $pod.$cluster-pxc -uroot -proot_password" "-3rd"
}

recreate() {
    run_mysql_local \
        '
            SET mysql-connect_timeout_server_max = 100500;
            LOAD MYSQL VARIABLES TO RUNTIME;
            SAVE MYSQL VARIABLES TO MEMORY;
            SAVE MYSQL VARIABLES TO DISK;
        ' \
        "-h127.0.0.1 -P6032 -uproxyadmin -padmin_password" "$cluster-proxysql-0"
    run_mysql_local \
        'SELECT * FROM global_variables ORDER BY variable_name' \
        "-h127.0.0.1 -P6032 -uproxyadmin -padmin_password" "$cluster-proxysql-0" \
        | grep -v ssor_regex \
        >$tmp_dir/proxy-vars1.sql

    local expected_result=$test_dir/compare/proxy-vars.sql
    if [[ "$IMAGE_PXC" =~ 8\.0 ]] && [ -f $test_dir/compare/proxy-vars-80.sql ]; then
        expected_result=$test_dir/compare/proxy-vars-80.sql
    fi
    diff -u $expected_result $tmp_dir/proxy-vars1.sql

    # delete cluster
    kubectl_bin delete \
        -f $conf_dir/$cluster.yml
    wait_for_delete pod/$cluster-pxc-2
    wait_for_delete pod/$cluster-pxc-1
    wait_for_delete pod/$cluster-pxc-0

    # recreate cluster
    apply_config "$conf_dir/$cluster.yml"

    wait_for_running $cluster-pxc 3
    compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster-pxc-0.$cluster-pxc -uroot -proot_password" "-3rd"
    compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster-pxc-1.$cluster-pxc -uroot -proot_password" "-3rd"
    compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster-pxc-2.$cluster-pxc -uroot -proot_password" "-3rd"

    sleep 60
    run_mysql_local \
        'SELECT * FROM global_variables ORDER BY variable_name' \
        "-h127.0.0.1 -P6032 -uproxyadmin -padmin_password" "$cluster-proxysql-0" \
        | grep -v ssor_regex \
        >$tmp_dir/proxy-vars2.sql

    local expected_result=$test_dir/compare/proxy-vars.sql
    if [[ "$IMAGE_PXC" =~ 8\.0 ]] && [ -f $test_dir/compare/proxy-vars-80.sql ]; then
        expected_result=$test_dir/compare/proxy-vars-80.sql
    fi
    diff -u $expected_result $tmp_dir/proxy-vars2.sql
}

main() {
    create_infra $namespace

    kubectl_bin apply -f "$conf_dir/pumba.yml"

    desc 'start cluster'
    spinup_pxc "$cluster" "$conf_dir/$cluster.yml"

    desc 'kill pod-0 pod'
    pumba_pod kill "$cluster-pxc-0"

    desc 'rm pod-0 pod'
    pumba_pod rm "$cluster-pxc-0"

    desc 'stop pod-0 pod'
    pumba_pod stop "$cluster-pxc-0"

    desc 'pause pod-0 pod'
    pause_pod "$cluster-pxc-0"

    desc 'emulate bad network pod-0 pod'
    netem_pod "$cluster-pxc-0"

    desc 'recreate cluster'
    recreate

    # pumba should be run on all pxc
    #desc 'kill all PXC pods'
    ## TODO: pumba_all_pods kill
    #desc 'rm all PXC pods'
    ## TODO: pumba_all_pods rm
    #desc 'stop all PXC pods'
    ## TODO: pumba_all_pods stop

    destroy $namespace
}

main
